/*
 * ------------------------------------------------------------------
 * Copyright © 2017, Hangzhou DtDream Technology Co.,Ltd. All rights reserved.
 * ------------------------------------------------------------------
 * Product : DTPortal
 * Module Name : test
 * Date Created: 2019/6/10 下午4:40
 * Description :
 *  ---------------------------------------------------------------------------------------------------------------
 *  Modification History * DATE Name Description
 *  ----------------------------------------------------------------------------------------------------------------
 */

package test;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.zwtConn.ECDSAUtil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class ExchangeDataDetailTest {

    public static void main(String[] args) throws Exception {

        // 数梦分配的应用id,仅测试使用
        String appId = "test";
        // 根据二维码格式选取的数梦提供的公钥
        String publicKey = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEYrDYXO97D4nsAiMdLHwDurG/t8APEZ8pts/eTb3KXyEaZpy7fJpW4GUAel43JKcCf49vO5/nZ720/Zw7EpkUcg==";
        // 自身的私钥
        String privateKey = "MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCC50oEg3W37vp5jpwHUXttZmlxJATJ65VVFHhuS9whJGQ==";
        // 自身的公钥
        // String publicKey="MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEW4C+xDBpQtzIdI8LZaDTFdeXizmZXyJgGTA3JgI+zN823Fh+lZyl6/ee8SIKrBkavrrLEpL9U1LKBYCyCvxFJQ=="
        // 不包含签名的二维码内容
        String qrCode = "00000000000001560142110321000060000000000000000000000000000000000000047a86db9c400000";
        // 构建报文内容
        Map<String, String> body = new HashMap<>(2);
        body.put("qrCode", qrCode);
        // 业务流水号可不传
        body.put("serialNumber", "123465");
        // json化报文
        String json = JSON.toJSONString(body);
        // 生成签名信息
        String sign = ECDSAUtil.signStr(json, privateKey);
        // 访问的目标地址,仅测试使用
        String path = "http://120.221.95.11:8007/app_api/qrCode/v1/exchange/dataDetail";
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 声明访问方法
        connection.setRequestMethod("POST");
        // 添加头部
        // 添加签名
        connection.setRequestProperty("sign", sign);
        // 添加应用id
        connection.setRequestProperty("appId", appId);
        // 添加应用id
        connection.setRequestProperty("Content-Type", "application/json");

        System.out.println("sign:"+sign);
        System.out.println("source:"+json);
        connection.setDoOutput(true);
        try (OutputStream outputStream = connection.getOutputStream()) {
            // 发送请求,为utf-8编码
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                // 获取响应报文的签名信息
                String responseSign = connection.getHeaderField("sign");
                try (InputStream inputStream = connection.getInputStream()){
                    // 获取回复报文
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,
                            StandardCharsets.UTF_8));
                    String strRead;
                    StringBuilder sb = new StringBuilder();
                    while ((strRead = reader.readLine()) != null) {
                        sb.append(strRead);
                    }
                    String result = sb.toString();
                    System.out.println(result);
                    // 解析响应报文
                    JSONObject jsonObject = JSON.parseObject(sb.toString());
                    if (jsonObject.getBoolean("success")) {
                        String data=jsonObject.getString("data");
                        // 对data内容进行验签
                        ECDSAUtil.verifyStr(data, publicKey, responseSign);
                        // TODO 根据具体业务对data进行处理
                        System.out.println(JSON.parseObject(data));
                    }
                }
            }
        }
    }
}
